<?php

namespace app\admin\controller;

use wechat\src\WeChat;

/**
 * 微信信息，
 */
class Wechatmessage extends base
{

    protected $wechat;

    //前置方法
    protected $beforeActionList = [
        'map' => ['only' => 'index'],
    ];

    /**
     * 搜索
     */
    protected function map()
    {
        if ($this->request->isGet()) {
            $data = $this->request->param();
            $this->datas = array();
            if (!empty($data['start_time']) && !empty($data['end_time']) && (strtotime($data['start_time']) < strtotime($data['end_time']))) {
                $this->datas ['create_time'] = [
                    ['>', strtotime($data['start_time'])],
                    ['<', strtotime($data['end_time'])]
                ];
            }
            if (isset($data['status'])) {
                $this->datas ['status'] = $data['status'];
            }

        }
    }


    /**
     * 显示用户留言内容
     * @param $id
     */
    public function read()
    {
        $get= $this->request->param();
        if(!empty($get['uid'])){
            $openid = model('wuser')->where('id',$get['uid'])->value('openid');
        }elseif(!empty($get['id'])){
            $openid = model('wechatmessage')->where('id',$get['id'])->value('openid');
        }

        $this->model->save(['status' => '1'], ['openid' => $openid]);

        $data = $this->model->where(['openid' => $openid])->find();
        /*列表start*/

        $list = model('wechatmessageread')
            ->where('openid', $openid)
            ->field('center,create_time,openid,who,mtype')
            ->order('create_time aSC')
            ->select();
        $this->assign([
            'list' => $list,
            'data' => $data,
            'openid' => $openid,
        ]);
        return $this->fetch();
    }

    /**
     * 发送消息
     */
    public function save()
    {

        $this->wechat = (new WeChat);
        $this->wechat->valid();
        //指定用户
        $d = $this->wechat->instance('Customservice')->sendTest($this->request->post('openid'), $this->request->post('center'));

        if ($d=='1') {
            $data = array(
                'openid' => $this->request->post('openid'),
                'center' => $this->request->post('center'),
                'who' => '1',
            );
            model('wechatmessageread')->save($data);
            return ajax_info('回复成功', 'index');
        }

        return ajax_info($d['errmsg']);
    }

    /**
     * 全部操作
     */
    public function dolistsave()
    {
        $post = $this->request->post();
        if (empty($post['id'])) {
            return ajax_info('请选择项目！', 'index');
        }
        foreach ($post['id'] as $key) {
            switch ($post['inputtype']) {
                case 'del':
                    $this->model->where('openid', $key)->delete();
                    model('wechatmessageread')->where('openid', $key)->delete();
                    break;
            }
        }
        return ajax_info('操作成功！', 'index');

    }

    /**
     * 发送消息
     */
    public function save11()
    {
        //指定用户
//        $data = "bbbbbbfdfdsvkcxvxc大师傅大师傅";
//        $res  = $this->wechat->instance('Customservice')->sendImage("otuUG0ztt5Wl_KnUs0T_dK9_2tXg",$data);
//        dump($res);
//        exit();
//        //指定用户
//        $data = "bbbbbbfdfdsvkcxvxc大师傅大师傅";
//        $res  = $this->wechat->instance('Customservice')->sendTest("otuUG0ztt5Wl_KnUs0T_dK9_2tXg",$data);
//        dump($res);
//        exit();

        //可以群发信息了

        $data = [
            "filter" => [
                "is_to_all" => true,
            ],

            "text" => [
                "content" => "你觉得呢？！成功了！要怎么利用呢？!1"
            ],
            "msgtype" => "text"
        ];
        $res = $this->wechat->instance('message')->sendAll($data);
        dump($res);
        exit();
        $dd = $this->wechat->instance('material')->upload('image', '../public/uploads/sm_avatar.png', 1);
//        $dd =$this->wechat->instance('material')->total();
        dump($dd);
        exit();
        $data = [
            "touser" => "otuUG0ztt5Wl_KnUs0T_dK9_2tXg",
            "mpnews" => [
                "media_id" => "vy-Bt5ys21PaDtHwY8DXhW2Z7KDpIU9_S1k2lA-4FGfDSoxdZBUkmM-Q9JFk-nRq",
            ],
            "msgtype" => "mpnews",
        ];
        $aa = $this->wechat->instance('message')->preview($data);
        dump($aa);

    }
}
